﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Framework
{
    /// <summary>
    /// 网络层，数据层扩展
    /// </summary>
    public abstract class Service : IService
    {
        private IEventTrigger _eventTrigger;
        private Dictionary<Type,IModel> _models;

        public abstract void Init();
        
        public Coroutine StartCoroutine(string routine)
        {
            return AssetManager.Instance.StartCoroutine(routine);
        }
        
        public Coroutine StartCoroutine(IEnumerator routine)
        {
            return AssetManager.Instance.StartCoroutine(routine);
        }

        public void Connect(IEventTrigger eventTrigger, Dictionary<Type, IModel> models)
        {
            _eventTrigger = eventTrigger;
            
            Init();
        }
        
        protected TModel GetModel<TModel>() where TModel : IModel
        {
            if (_models.TryGetValue(typeof(TModel), out IModel model))
            {
                return (TModel)model;
            }
            else
            {
                Log.Error("Model "+typeof(TModel).Name+"未注册!");
            }

            return default(TModel);
        }

        public virtual void Dispose()
        {
            
        }

        public void TriggerEvent(string eventName)
        {
            _eventTrigger.TriggerEvent(eventName);
        }

        public void TriggerEvent<T>(string eventName, T arg1)
        {
            _eventTrigger.TriggerEvent(eventName, arg1);
        }

        public void TriggerEvent<T, U>(string eventName, T arg1, U arg2)
        {
            _eventTrigger.TriggerEvent(eventName, arg1, arg2);
        }

        public void TriggerEvent<T, U, V>(string eventName, T arg1, U arg2, V arg3)
        {
            _eventTrigger.TriggerEvent(eventName, arg1, arg2, arg3);
        }

        public void TriggerEvent<T, U, V, W>(string eventName, T arg1, U arg2, V arg3, W arg4)
        {
            _eventTrigger.TriggerEvent(eventName, arg1, arg2, arg3, arg4);
        }
    }
}